let db = wx.cloud.database()
// 检索数据表
let subject = db.collection("subject")
Page({
  data: {
    v1: "",
    v2: "",
    level: "初级",
    type: "",
    num: 0,
    typeList: [
      {type:'html',text:"HTML"},
      {type:'css',text:"CSS"},
      {type:'js',text:"JavaScript"},
      {type:'vue',text:"Vue"},
      {type:'webpack',text:"Webpack"}
    ],
    isShow: false,
    message:"请选择类型"
  },
  addInput() {},
  addToggle(){
    this.setData({
      isShow:  !this.data.isShow
    })
  },
  addSelect(event){
    let _type = event.currentTarget.dataset.type;
    let _text = event.currentTarget.dataset.text;
    // console.log(_type);
    this.setData({
      type: _type,
      message: _text,
      isShow: false
    })
  },
  addClick(event){
    let level = event.currentTarget.dataset.level;
    let num = event.currentTarget.dataset.num;
    // console.log(level,num);
    this.setData({
      level,
      num
    })
  },
  addPublish() {
    wx.showLoading({
      title: '正在发布',
    })
    let question = this.data.v1; // 题目
    let answer = this.data.v2; // 答案
    let level = this.data.level; // 等级水平
    let type = this.data.type; // 类型 html css
    let hot = false;
    let collect = false;
    let record = false;
    let color;
    let _lsrc;
    let src;
    // console.log(level,type);
    if(level == '初级'){
      _lsrc = "https://636c-cloud1-2g73mn8d879c3-5b7cfc471b7-1309151763.tcb.qcloud.la/%E5%9B%BE%E7%89%87%E5%9B%BE%E6%A0%87/chu2.png?sign=38fdd2f913efd79ebcfa9198c1a750c8&t=1645097541"
    }else if(level == '中级'){
      _lsrc = "https://636c-cloud1-2g73mn8d879c3-5b7cfc471b7-1309151763.tcb.qcloud.la/%E5%9B%BE%E7%89%87%E5%9B%BE%E6%A0%87/zhong2.png?sign=68a5d7f9023fac2263185efd1838c197&t=1645097566"
    }else{
      _lsrc = "https://636c-cloud1-2g73mn8d879c3-5b7cfc471b7-1309151763.tcb.qcloud.la/%E5%9B%BE%E7%89%87%E5%9B%BE%E6%A0%87/gao2.png?sign=3842710d5bb1298b6d3e9da439678974&t=1645097581"
    }
    if(type == 'html'){
      color = '#fc490b';
      src = 'https://636c-cloud1-2g73mn8d879c3-5b7cfc471b7-1309151763.tcb.qcloud.la/%E5%9B%BE%E7%89%87%E5%9B%BE%E6%A0%87/html-2.png?sign=9286aadfdbc922e32a00941c398e7ab7&t=1645097619'
    }else if(type == 'css'){
      color = '#1296db';
      src = 'https://636c-cloud1-2g73mn8d879c3-5b7cfc471b7-1309151763.tcb.qcloud.la/%E5%9B%BE%E7%89%87%E5%9B%BE%E6%A0%87/css-2.png?sign=0696ce2f1eaf89d7802413cc823e9434&t=1645097633'
    }else if(type == 'js'){
      color = '#f4ea2a';
      src = 'https://636c-cloud1-2g73mn8d879c3-5b7cfc471b7-1309151763.tcb.qcloud.la/%E5%9B%BE%E7%89%87%E5%9B%BE%E6%A0%87/js-2.png?sign=09b4f1f07d431736d2c0a26d09958b3f&t=1645097645'
    }else if(type == 'vue'){
      color = '#41b883';
      src = 'https://636c-cloud1-2g73mn8d879c3-5b7cfc471b7-1309151763.tcb.qcloud.la/%E5%9B%BE%E7%89%87%E5%9B%BE%E6%A0%87/Vue-2.png?sign=ef384321a79f408cf0ce61cfd3e2f378&t=1645097680'
    }else{
      color = '#8ed6fb';
      src = 'https://636c-cloud1-2g73mn8d879c3-5b7cfc471b7-1309151763.tcb.qcloud.la/%E5%9B%BE%E7%89%87%E5%9B%BE%E6%A0%87/Webpack-2.png?sign=a54871b27b2b55ed642fa62efdef5b5e&t=1645097691'
    }
    let _id = Math.random().toString().slice(2) + "_" + new Date().getTime();
    if (question.length == 0 ||
      answer.length == 0 ||
      level.length == 0 ||
      type.length == 0) {
      wx.showToast({
        icon: "error",
        title: '输入框不能为空!'
      })
      return;
    }
    subject.add({
        data: {
          _id,
          question,
          answer,
          level,
          type,
          hot,
          collect,
          record,
          _lsrc,
          color,
          src
        }
      })
      .then(
        res => {
          wx.hideLoading();
          wx.showToast({
            title: '发布成功',
          })
          this.setData({
            message : '请选择类型'
          })
          // console.log(res);
          // wx.showModal({
          //   title: '发布成功,是否跳转?',
          //   success() {
          //     // 跳转页面
          //     wx.navigateTo({
          //       url: '/pages/mymassage/mymassage.wxml',
          //     })
          //   }
          // })
        }
      )

      this.setData({v1:"",v2:""})
  }
})